package com.example.agron.user;

import com.example.agron.common.http.UserResponse;
import com.example.agron.common.utils.APIResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@RestController
@RequestMapping(path = "system/user")
public class UserController {

    private final  UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    /*@PostConstruct
    public void initDB(){
        List<User> users = IntStream.rangeClosed(1, 200)
                .mapToObj(i -> new User("user"+1, new Random().nextInt(100)))
                .collect(Collectors.toList());
    }*/



    /**
     * 查询用户列表
     * @return
     * */
    @GetMapping("/list")
    public UserResponse<User> getUsers(@RequestParam int offset, @RequestParam int pageSize, @RequestParam String field){
        return userService.getUsers(offset, pageSize, field);
    }

    /**
     * 查询用户列表
     * @return
     * */
    @GetMapping("/search")
    public UserResponse<User> searchUsers(
            @RequestParam int offset,
            @RequestParam int pageSize,
            @RequestParam String name,
            @RequestParam String email,
            @RequestParam String gender,
            @RequestParam String fieldBy,
            @RequestParam String field
    ){
        return userService.searchUsers(offset, pageSize, name, email, gender, fieldBy, field);
    }


    /**
     * 用户列表排序
     * @return
     * */
    @GetMapping("")
    private List<User> getUsersWithSorting(@RequestParam String field){
        return userService.findUsersWithSorting(field);
    }


    /**
     * 查询用户信息
     * @param userId
     * @return
     * */
    @GetMapping(path = "{userId}")
    public ResponseEntity<User> getUserById(@PathVariable("userId") Long userId){
        return userService.getUserById(userId);
    }


    /**
     * 新增用户
     * @param user
     * @return
     * */
    @PostMapping
    public void registerNewUser(@RequestBody User user){
        userService.addNewUser(user);
    }


    /**
     * 删除用户
     * @param userId
     * @return
     * */
    @DeleteMapping(path = "{userId}")
    public void deleteUser(@PathVariable("userId") Long userId){
        userService.deleteUser(userId);
    }

    /**
     * 批量删除用户
     * @param userIds
     * @return
     * */
    @DeleteMapping(path = "/batch/{userIds}")
    public void batchDeleteUser(@PathVariable("userIds") String userIds){
        userService.batchDeleteUser(userIds);
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     * */
    @PutMapping(path = "")
    public UserResponse<User> updateUser(@RequestBody User user){
        return userService.updateUser(user.getId(), user.getName(), user.getPassword(), user.getEmail(), user.getAvatarUrl(), user.getGender(), user.getDob());
    }


    /**
     * 头像上传
     * @param headerImage
     * @param model
     * @return
     * */
    @PostMapping(path = "/uploadAvatar")
    public void uploadUserAvatar(MultipartFile headerImage, Model model){
        userService.uploadUserAvatar(headerImage, model);
    }


    /**
     * 图片上传
     * @param file
     * @param model
     * @return
     * */
    @PostMapping("/uploadImage")
    public void uploadUserImage(@RequestParam("file") MultipartFile file, Model model){
        System.err.println(file.getName());
        userService.uploadUserImage(file, model);
    }

}
